﻿using System.Linq;
using CrmService.Enums;
using CrmService.Models.Data;
using System.Collections.Generic;
using CrmService.Repository.Interfaces;

namespace CrmService.Repository
{
    /// <summary>
    ///     This class interacts with the Crm DB to perform CRUD operations on the Idea entity
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable",Justification="DataContext does not hold expensive resources. Will be garbage collected automatically.")]
    public class CrmRepository : ICrmRepository
    {
        #region ICrmRepository Members

        /// <summary>
        ///     Instance of DataContext to connect to the Crm DB
        /// </summary>
        private readonly CrmDataContext _dataContext = new CrmDataContext();

        /// <summary>
        ///     Method to add Ideas to the Crm DB
        /// </summary>
        /// <param name="ideas">
        ///     The list of ideas to be added
        /// </param>
        public void AddIdeas(IEnumerable<Idea> ideas)
        {
            _dataContext.Ideas.InsertAllOnSubmit(ideas);
            Update();
        }

        /// <summary>
        ///     Method to delete an idea from the Crm DB
        /// </summary>
        /// <param name="idea">
        ///     The Idea to be deleted
        /// </param>
        public void DeleteIdea(Idea idea)
        {
            _dataContext.Ideas.DeleteOnSubmit(idea);
            Update();
        }

        /// <summary>
        ///     Method to find ideas for a given status
        /// </summary>
        /// <param name="status">
        ///     The status to be considered
        /// </param>
        /// <returns>
        ///     The list of ideas for the given status
        /// </returns>
        public IEnumerable<Idea> FindIdeasByStatus(IdeaStatus status)
        {
            return from idea in _dataContext.Ideas
                   where idea.Status == (byte)status
                   select idea;
        }

        /// <summary>
        ///     Method to find an idea by its ID
        /// </summary>
        /// <param name="ideaId">
        ///     The idea ID for the idea to be found
        /// </param>
        /// <returns>
        ///     The idea for the given id
        /// </returns>
        public Idea FindById(int ideaId)
        {
            return _dataContext.Ideas.Single(i => i.IdeaId == ideaId);
        }

        /// <summary>
        ///     Method to submit all tracked changes
        /// </summary>
        public void Update()
        {
            _dataContext.SubmitChanges();
        }

        /// <summary>
        ///     Method to get the list of all Ideas in the Crm DB
        /// </summary>
        /// <returns>
        ///     List of all Ideas.
        /// </returns>
        public IEnumerable<Idea>GetAllIdeas()
        {
            return _dataContext.Ideas.ToList();
        }
        #endregion
    }
}
